// 最小化安全增强 - 绝对不会报错的版本
(function() {
    'use strict';
    
    console.log('🔒 最小化安全增强启动...');
    
    // 全局错误捕获，防止任何错误影响游戏
    const safeExecute = (fn) => {
        try {
            return fn();
        } catch (error) {
            // 静默处理所有错误
            return null;
        }
    };
    
    class MinimalSafeEnhancements {
        constructor() {
            this.enabled = false;
            this.dodgeCooldown = 0;
            this.comboCount = 0;
            this.lastKillTime = 0;
            this.lastZombieCount = 0;
            
            // 延迟初始化，给游戏充分时间加载
            setTimeout(() => this.initialize(), 5000);
        }
        
        initialize() {
            console.log('🛡️ 最小化安全初始化...');
            
            // 只启用绝对安全的功能
            this.setupBasicDodge();
            this.setupBasicStatus();
            this.setupBasicCombo();
            
            this.enabled = true;
            console.log('✅ 最小化增强已安全启用');
        }
        
        setupBasicDodge() {
            // 最基础的闪避功能
            document.addEventListener('keydown', (e) => {
                if (e.code === 'Space' && this.dodgeCooldown <= 0) {
                    e.preventDefault();
                    this.basicDodge();
                }
            });
            
            // 简单的冷却倒计时
            setInterval(() => {
                if (this.dodgeCooldown > 0) {
                    this.dodgeCooldown -= 100;
                }
            }, 100);
        }
        
        basicDodge() {
            console.log('🏃 基础闪避');
            this.dodgeCooldown = 1000;
            
            // 最简单的视觉反馈
            this.showBasicFlash();
            
            // 安全的无敌效果
            safeExecute(() => {
                if (window.player) {
                    window.player.invulnerable = true;
                    setTimeout(() => {
                        safeExecute(() => {
                            if (window.player) {
                                window.player.invulnerable = false;
                            }
                        });
                    }, 300);
                }
            });
        }
        
        showBasicFlash() {
            // 全屏闪光效果已禁用
            return;
        }
        
        setupBasicStatus() {
            const status = document.createElement('div');
            status.id = 'minimalStatus';
            status.style.cssText = `
                position: fixed; top: 10px; right: 10px; z-index: 9999;
                background: rgba(0,0,0,0.7); color: white; padding: 8px;
                border-radius: 4px; font-family: monospace; font-size: 10px;
                border: 1px solid #4ECDC4;
            `;
            status.innerHTML = `
                <div style="color: #4ECDC4;">🎮 增强</div>
                <div id="miniDodge">🏃 就绪</div>
                <div id="miniCombo">🔥 0x</div>
            `;
            
            document.body.appendChild(status);
            
            setInterval(() => this.updateBasicStatus(), 200);
        }
        
        updateBasicStatus() {
            safeExecute(() => {
                const dodgeEl = document.getElementById('miniDodge');
                const comboEl = document.getElementById('miniCombo');
                
                if (dodgeEl) {
                    dodgeEl.textContent = this.dodgeCooldown > 0 ? 
                        `🏃 ${(this.dodgeCooldown/1000).toFixed(1)}s` : '🏃 就绪';
                }
                
                if (comboEl) {
                    comboEl.textContent = `🔥 ${this.comboCount}x`;
                }
                
                // 连击超时
                const now = Date.now();
                if (this.comboCount > 0 && now - this.lastKillTime > 3000) {
                    this.comboCount = 0;
                }
            });
        }
        
        setupBasicCombo() {
            // 非常保守的击杀检测
            setInterval(() => {
                safeExecute(() => {
                    if (window.gameEngine && 
                        window.gameEngine.zombies && 
                        window.gameEngine.gameState === 'playing') {
                        
                        const currentCount = window.gameEngine.zombies.length;
                        
                        if (this.lastZombieCount > 0 && currentCount < this.lastZombieCount) {
                            const kills = this.lastZombieCount - currentCount;
                            for (let i = 0; i < kills; i++) {
                                this.basicKill();
                            }
                        }
                        
                        this.lastZombieCount = currentCount;
                    }
                });
            }, 300);
        }
        
        basicKill() {
            const now = Date.now();
            
            if (now - this.lastKillTime > 3000) {
                this.comboCount = 1;
            } else {
                this.comboCount++;
            }
            
            this.lastKillTime = now;
            
            console.log(`🔥 基础连击: ${this.comboCount}x`);
            
            // 基础连击显示
            this.showBasicCombo();
            
            // 基础奖励
            if (this.comboCount % 5 === 0) {
                this.basicReward();
            }
        }
        
        showBasicCombo() {
            const combo = document.createElement('div');
            combo.style.cssText = `
                position: fixed; top: 30%; left: 50%; transform: translateX(-50%);
                font-size: 24px; color: #FFD700; font-weight: bold;
                text-shadow: 2px 2px 4px rgba(0,0,0,0.8); z-index: 7000;
                pointer-events: none; font-family: monospace;
            `;
            combo.textContent = `${this.comboCount}x`;
            
            document.body.appendChild(combo);
            
            setTimeout(() => {
                safeExecute(() => {
                    if (combo.parentNode) {
                        document.body.removeChild(combo);
                    }
                });
            }, 1000);
        }
        
        basicReward() {
            safeExecute(() => {
                if (window.player && window.player.health < window.player.maxHealth) {
                    window.player.health = Math.min(window.player.maxHealth, window.player.health + 8);
                    safeExecute(() => {
                        if (window.player.updateHealthUI) {
                            window.player.updateHealthUI();
                        }
                    });
                    
                    this.showBasicReward();
                }
            });
        }
        
        showBasicReward() {
            const reward = document.createElement('div');
            reward.style.cssText = `
                position: fixed; top: 20%; left: 50%; transform: translateX(-50%);
                font-size: 16px; color: #00FF00; font-weight: bold;
                text-shadow: 2px 2px 4px rgba(0,0,0,0.8); z-index: 7000;
                pointer-events: none; font-family: monospace;
            `;
            reward.textContent = '+8 HP';
            
            document.body.appendChild(reward);
            
            setTimeout(() => {
                safeExecute(() => {
                    if (reward.parentNode) {
                        document.body.removeChild(reward);
                    }
                });
            }, 1500);
        }
    }
    
    // 最安全的启动方式
    safeExecute(() => {
        window.minimalSafeEnhancements = new MinimalSafeEnhancements();
        console.log('✅ 最小化安全增强已加载');
    });
    
})();